package club.cearnach.web.controller.admin;

import club.cearnach.web.controller.BaseController;
import club.cearnach.api.dto.PersonDTO;
import club.cearnach.entity.Person;
import club.cearnach.api.exception.MoviePathException;
import club.cearnach.api.exception.PersonException;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.*;
import club.cearnach.api.service.impl.PersonServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author 阮胜
 * @date 2018/5/12 17:01
 */
@Api(tags = "后台人物控制类")
@Controller
@RequestMapping("/admin/person")
@Slf4j
public class BackgroundPersonController extends BaseController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private CacheManager cacheManager;

    public BackgroundPersonController(IActorService actorService, IAdminService adminService, IDirectorService directorService, IMovieLanguageService movieLanguageService, IMovieService movieService, IMovieTypeService movieTypeService, IPersonService personService, IPlaceService placeService, IVisitorService visitorService) {
        super(actorService, adminService, directorService, movieLanguageService, movieService, movieTypeService, personService, placeService, visitorService);
    }

    @PostMapping("/save")
    @ResponseBody
    public boolean save(@Valid PersonDTO personDTO) throws PersonException, IOException, MoviePathException {
        personService.save(personDTO);
        return true;
    }


    @GetMapping("/list")
    public String list(PageBean pageBean, Model model) {
        cacheManager.getCache(PersonServiceImpl.CACHE_PERSON_FIND_ALL).clear();
        Page<Person> personPage = personService.findAllOrderByCreateDate(pageBean);
        model.addAttribute(personPage.getContent());
        model.addAttribute("totalPage", personPage.getTotalPages());
        model.addAttribute("currentPage", pageBean.getPage());
        return "admin/person-list";
    }

    @ApiOperation("跳转到编辑人物页面")
    @GetMapping("/edit/form/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) throws PersonException {
        cacheManager.getCache(PersonServiceImpl.CACHE_PERSON_NAME).evict("person_" + id);
        Person person = personService.findById(id);
        model.addAttribute(person);
        return "admin/person-save";
    }

    @ApiOperation("删除人物信息")
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") Integer id) throws PersonException {
        personService.delete(id);
        return true;
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/delete/all")
    @ResponseBody
    public long deleteAll(@RequestParam(value = "id") Integer[] idArr) {
        return personService.deleteIn(idArr);
    }


}


